/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 19-12-11 上午10:07
 * History:
 */
package com.swak.frame.excel;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.core.io.Resource;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.swak.frame.logger.SwakLoggerFactory;
import com.swak.frame.util.StringPool;


/**
 * AbstractLoaderSupportFactory.java
 * 
 * @author ColleyMa
 * @version 19-12-11 上午10:07
 */
public abstract class AbstractLoaderSupportFactory implements LoaderSupportFactory {
    protected Logger logger = SwakLoggerFactory.getLogger(getClass()); // 日志记录

    protected Resource[] locations;
    protected String fileEncoding;
    protected Map<String, Map<String, String>> localProp;

    public void setLocation(Resource location) {
        this.locations = new Resource[] {location};
    }

    public void setLocations(Resource... locations) {
        this.locations = locations;
    }

    public void setFileEncoding(String fileEncoding) {
        this.fileEncoding = fileEncoding;
    }

    @Override
    public String getProperty(String key, String celKey) {
        Map<String, String> prop = localProp.get(key);

        if (MapUtils.isNotEmpty(prop)) {
            return prop.get(celKey);
        }

        return null;
    }

    @Override
    public <T> T getProperty(String key, Class<T> clazz) {
        Map<String, String> prop = localProp.get(key);

        if (MapUtils.isEmpty(prop)) {
            prop = Maps.newHashMap();
        }

        return JSON.parseObject(JSON.toJSONString(prop), clazz);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.localProp = Maps.newHashMap();

        if (StringUtils.isEmpty(fileEncoding)) {
            this.fileEncoding = StringPool.UTF8;
        }

        this.loadProp(this.localProp);
    }

    @Override
    public Map<String, Map<String, String>> getLocalProp() {
        return localProp;
    }

    @Override
    public List<Map<String, String>> getLocalPropList() {
        return Lists.newArrayList(localProp.values());
    }
}
